package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.AdminRoleMapper;
import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 杨林
 * @create 2023-07-06 9:59 星期四
 * description:
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     * 获取角色分页列表
     * @param roleQueryVo
     * @param
     * @return
     */
    @Override
    public IPage<Role> selectPage(RoleQueryVo roleQueryVo, Long page,Long limit) {
        //获取查询条件
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)){
            wrapper.like(Role::getRoleName,roleName);
        }
        Page<Role> pageModel = roleMapper.selectPage(new Page<>(page, limit), wrapper);
        return pageModel;
    }

    /**
     * 获取所有角色 和 根据用户id获取当前用户的所有角色
     * @param adminId
     * @return
     */
    @Override
    public Map<String, Object> getRoleAndUserRole(Long adminId) {
        Map<String,Object> result = new HashMap<>();
        //获取所有角色
        List<Role> allRoleList = roleMapper.selectList(null);
        //注意：map的key（allRolesList） 需要和前端取值时的key保持一致
        result.put("allRolesList",allRoleList);
        //根据用户id获取用户对应的角色ids
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);

        List<AdminRole> adminRoles = adminRoleMapper.selectList(wrapper);
        //如下写法会多次访问数据库，造成数据库压力
        //注意：非必要情况下，不再循环中访问数据库，避免对数据库造成压力
//        List<Role> list = new ArrayList<>();
//        for (AdminRole adminRole : adminRoles) {
//            Long roleId = adminRole.getRoleId();
//            LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Role::getId,roleId);
//            Role role = roleMapper.selectOne(queryWrapper);
//            list.add(role);
//        }
        //优化
        //利用stream的方式得到当前用户对应的角色id集合
        List<Long> roleIds = adminRoles.stream().map(AdminRole::getRoleId)
                .collect(Collectors.toList());
        //方案1：不再访问数据库，通过遍历所有的角色，获取对应角色id的角色
//        List<Role> adminRole = new ArrayList<>();
//        for (Long roleId : roleIds) {
//            for (Role role : allRoleList) {
//                if (roleId == role.getId()){
//                    adminRole.add(role);
//                }
//            }
//        }
        //对以上代码进一步优化
//        for (Role role : allRoleList) {
//            if (roleIds.contains(role.getId())){  //判断集合中是否包含该元素
//                adminRole.add(role);
//            }
//        }

        //方案2：通过SQL获取对应的角色集合
//        List<Role> adminRole  = roleMapper.selectRoleByIds(roleIds);

        //方案3：通过MybatisPlus构建查询条件，代替方案二中的查询条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Role::getId,roleIds);
        List<Role> adminRole = roleMapper.selectList(queryWrapper);

        result.put("assignRoles",adminRole);
        return result;
    }
}
